﻿using System;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine;
using SCG.SolarVengeanceEngine.BrainInfo;

namespace SCG.SolarVengeance.Brains
{
    public class Striker : Brain
    {
        public override ChallengeLevel Challenge
        {
            get
            {
                return ChallengeLevel.Moderate;
            }
        }

        public override void Execute()
        {
            //Do we have the WarShip?
            StarShipInfo theWarShip = null;
            foreach(StarShipInfo ship in MyStarShips)
                if (ship.ShipType == StarShipType.WarShip)
                {
                    theWarShip = ship;
                    break;
                }

            if (theWarShip == null)
            {
                //build the warship
                SetAutoScanners(Capital, false);
                SetAutoStarShip(Capital, false);
                BuildStarShip(Capital, StarShipType.WarShip, 20, 13, false);
            }
            else
            {
                //we have the warship - are enemies near capital?
                StarShipInfo enemy = ClosestStarShip(Capital.X, Capital.Y, Capital.Scanners, FindShipFlags.Enemy, ShipKnownFlags.Either);
                if (enemy == null)
                {
                    SetAutoStarShip(Capital, false);
                    SetAutoScanners(Capital, true);
                }
                else
                {
                    SetAutoScanners(Capital, false);
                    SetAutoStarShipType(Capital, StarShipType.Drone, 4, 0, false);
                    SetAutoStarShip(Capital, true);
                }

                //see if capital was identified
                foreach(StarSystemInfo sys in StarSystems)
                    if (!sys.OwnedByMe && sys.IsCapital)
                    {
                        _enemyCapital = sys;
                        break;
                    }

                //Was capital identified?
                if (_enemyCapital != null)
                {
                    //are we adjacent?
                    if (DistanceQuick(_enemyCapital.X, _enemyCapital.Y, theWarShip.X, theWarShip.Y) <= 1)
                    {
                        AttackAdjacent(theWarShip, false);
                        Target(theWarShip, _enemyCapital);
                    }
                    else
                    {
                        AttackAdjacent(theWarShip, true);
                        Target(theWarShip, _enemyCapital);
                    }
                }
                else
                {
                    //flag starsystems that are visible as explored
                    foreach (StarSystemInfo sys in StarSystems)
                        if (sys.Visible)
                            if (!_explored.Contains(sys))
                                _explored.Add(sys);

                    //explore next closest unexplored system
                    double dist = Double.MaxValue;
                    StarSystemInfo explore = null;
                    foreach(StarSystemInfo unexplored in StarSystems)
                        if (!_explored.Contains(unexplored))
                        {
                            double d = Distance(theWarShip.X, theWarShip.Y, unexplored.X, unexplored.Y);
                            if (d < dist)
                            {
                                dist = d;
                                explore = unexplored;
                            }
                        }
                    if (explore != null)
                        Move(theWarShip, explore.X, explore.Y);
                }
            }

            //process other warships we might have
            foreach(StarShipInfo ship in StarShips)
                if (ship.OwnedByMe && ship != theWarShip)
                {
                    if (ship.CanTargetStarSystems)
                    {
                        StarSystemInfo sys = ClosestStarSystem(ship.X, ship.Y, Int32.MaxValue, FindSystemFlags.Unfriendly);
                        if (sys != null)
                            Target(ship, sys);
                    }
                    else
                    {
                        Defend(ship, ship);
                    }
                }

            //use other resources
            if (theWarShip != null)
            {
                foreach (StarSystemInfo sys in StarSystems)
                    if (sys.OwnedByMe)
                        BuildStarShip(sys, StarShipType.WarShip, Random(10, 20), Random(4, 20), false);
            }
        }

        public override string Description
        {
            get
            {
                return "Can a Brain win by building 1 WarShip?";
            }
        }

        private List<StarSystemInfo> _explored = new List<StarSystemInfo>();
        private StarSystemInfo _enemyCapital = null;
    }
}
